package com.wswy.wzcx.ui.pay

import android.os.Bundle
import android.text.format.DateFormat
import com.wswy.wzcx.R
import com.wswy.wzcx.api.ConfigsKt
import com.wswy.wzcx.model.DataCenter
import com.wswy.wzcx.model.home.RPaths
import com.wswy.wzcx.model.home.RouterUtils
import com.wswy.wzcx.module.GlobalConfigManager
import com.wswy.wzcx.module.base.CBaseFragment
import com.wswy.wzcx.module.base.gone
import com.wswy.wzcx.module.base.onClick
import com.wswy.wzcx.module.base.visible
import com.wswy.wzcx.module.pay.PayManager
import com.wswy.wzcx.ui.Constants
import com.wswy.wzcx.ui.web.WebViewActivity
import kotlinx.android.synthetic.main.fragment_order_pay_success.*


/**
 * 支付成功
 */
class PaySuccessFragment : CBaseFragment() {

    companion object {
        const val TAG = "PaySuccessFragment"
    }

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_order_pay_success
    }

    override fun getFragmentTitle(): String {
        return "支付结果"
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        arguments?.getParcelable<PayComplete?>(Constants.EXTRA_DATA)?.let {
            initViews(it)
            initData(it)
        }?:kotlin.run {

            finish()
        }

    }

    private fun initViews(payComplete: PayComplete){

        tv_step_money.text = getString(R.string.amount_args,payComplete.amount)


        val type = when(payComplete.type){
            PayOrder.TYPE_WZDB -> PayManager.PAY_TYPE_WZDB
            else -> PayManager.PAY_TYPE_NORMAL
        }

        when(payComplete.type){
            PayOrder.TYPE_WZDB -> {
                tv_step_title1.text = "支付成功，请注意遵守交通法律法规"
                tv_step_title2.text = "办理完成"
                img_step_1.setImageResource(R.drawable.order_paysucessed)
                img_step_2.setImageResource(R.drawable.pay_order_money)
            }
            PayOrder.TYPE_FKDJ -> {
                tv_step_title1.text = "支付成功，请注意遵守交通法律法规"
                tv_step_title2.text = "办理完成"

                img_step_1.setImageResource(R.drawable.order_paysucessed)
                img_step_2.setImageResource(R.drawable.pay_order_money)
            }
            PayOrder.TYPE_NJDB -> {
                //显示状态3
                img_step_3.visible()
                view_line_2.visible()
                tv_step_title3.visible()
                tv_step_hint_3.visible()

                img_step_1.setImageResource(R.drawable.order_paysucessed)
                img_step_2.setImageResource(R.drawable.pay_order_money)
                img_step_3.setImageResource(R.drawable.pay_order_mark)

                tv_step_title1.text = "支付成功"
                tv_step_title2.text = "年检办理"

                tv_step_title3.text = "回寄标志"
                tv_step_hint_3.text = "待年检办理完成，我们将把标志邮寄给您。"
            }
        }

        PayManager.getPayStyleMode(payComplete.payType?:0,type)?.let {
            tv_pay_info.text = getString(R.string.pay_success_type_hint,it.title,DateFormat.format("yyyy-MM-dd HH:mm",System.currentTimeMillis()))
        }?:kotlin.run {
            tv_pay_info.text = getString(R.string.pay_success_type_hint,"",DateFormat.format("yyyy-MM-dd HH:mm",System.currentTimeMillis()))
        }

        tv_pay_ok_hint.text = when(payComplete.type){
            PayOrder.TYPE_WZDB -> getString(R.string.pay_success_msg_wzdb)
            PayOrder.TYPE_FKDJ -> getString(R.string.pay_success_msg_fdjf)
            PayOrder.TYPE_NJDB -> getString(R.string.pay_success_msg_njdb)
            else -> ""
        }
        tv_submit.onClick {
            WebViewActivity.startWebview(context, ConfigsKt.H5_ORDER_LIST().format(0, DataCenter.get().userMode?.token))
            
        }

    }


    private fun initData(payComplete: PayComplete){

        tv_go_home.text ="返回首页"
        tv_go_home.onClick {
            finish()
        }

        GlobalConfigManager.getInstance().sysConf.getWskfWechat()?.let {

            val k = when(payComplete.type){
                PayOrder.TYPE_WZDB -> "20"
                PayOrder.TYPE_FKDJ -> "10"
                PayOrder.TYPE_NJDB -> "30"
                else -> null
            }

            if(!k.isNullOrEmpty()){
                //正确
                val value = it.get(k)

                if (!value.isNullOrEmpty()){
                    //有客服微信配置

                    tv_go_home.text ="联系客服"
                    tv_go_home.onClick {

                        RouterUtils.start(requireContext(),RPaths.KF_WECHAT,Bundle().apply {
                            putString(Constants.EXTRA_DATA,value)
                        })

                    }


                }
            }

        }
    }


}